<?php

use app\admin\Service;
use think\admin\extend\PhinxExtend;
use think\admin\model\SystemConfig;
use think\admin\model\SystemUser;
use think\migration\Migrator;
use app\helper\CommonHelper;

@set_time_limit(0);
@ini_set('memory_limit', -1);

/**
 * 系统模块初始化
 */
class InstallAdminData extends Migrator
{
    /**
     * 数据库初始化
     * @return void
     */
    public function change()
    {
        $this->insertUser();
        $this->insertMenu();
        $this->insertConf();
    }

    /**
     * 初始化用户数据
     * @return void
     */
    private function insertUser()
    {
        $model = SystemUser::mk()->whereRaw('1=1')->findOrEmpty();

        if($model->isEmpty()){
            $password  = CommonHelper::get_rand_str();
            $file = fopen("database/secret.txt", "w");
            fwrite($file, "进入系统后请及时修改账号密码并【删除该文件】\r\n账号：admin，密码：" . $password);
            fclose($file);

            $model->save([
                'id' => '10000',
                'username' => 'admin',
                'nickname' => '超级管理员',
                'password' => md5($password),
                'headimg'  => 'https://doc.thinkadmin.top/static/img/head.png',
            ]);
        }
    }

    /**
     * 初始化配置参数
     * @return void
     */
    private function insertConf()
    {
        $modal = SystemConfig::mk()->whereRaw('1=1')->findOrEmpty();
        $modal->isEmpty() && $modal->insertAll([
            ['type' => 'base', 'name' => 'app_name', 'value' => 'Reseed Puppy'],
            ['type' => 'base', 'name' => 'app_version', 'value' => 'v1'],
            ['type' => 'base', 'name' => 'editor', 'value' => 'ckeditor5'],
            ['type' => 'base', 'name' => 'login_name', 'value' => 'Reseed Puppy'],
            ['type' => 'base', 'name' => 'site_copy', 'value' => 'Reseed Puppy'],
            ['type' => 'base', 'name' => 'site_icon', 'value' => 'https://doc.thinkadmin.top/static/img/logo.png'],
            ['type' => 'base', 'name' => 'site_name', 'value' => 'Reseed Puppy'],
            ['type' => 'base', 'name' => 'site_theme', 'value' => 'white'],
            ['type' => 'storage', 'name' => 'type', 'value' => 'local'],
            ['type' => 'storage', 'name' => 'allow_exts', 'value' => 'doc,gif,ico,jpg,mp3,mp4,p12,pem,png,zip,rar,xls,xlsx'],
        ]);
    }

    /**
     * 初始化系统菜单
     * @return void
     */
    private function insertMenu()
    { // 解析并初始化菜单数据
        $json = '[
        {
            "name": "系统管理",
            "icon": "",
            "url": "#",
            "node": "",
            "params": "",
            "subs": [
                {
                    "name": "下载器配置",
                    "icon": "iconfont iconfont-down",
                    "url": "admin/download/index",
                    "node": "admin/download/index",
                    "params": ""
                },
                {
                    "name": "站点配置",
                    "icon": "iconfont iconfont-discover",
                    "url": "admin/sites/index",
                    "node": "admin/sites/index",
                    "params": ""
                },
                {
                    "name": "定时任务",
                    "icon": "layui-icon layui-icon-log",
                    "url": "admin/queue/index",
                    "node": "admin/queue/index",
                    "params": ""
                },
                {
                    "name": "系统配置",
                    "icon": "",
                    "url": "#",
                    "node": "",
                    "params": "",
                    "subs": [
                        {
                            "name": "系统日志管理",
                            "url": "admin/oplog/index",
                            "node": "admin/oplog/index",
                            "icon": "layui-icon layui-icon-form",
                            "params": ""
                        },
                        {
                            "name": "系统菜单管理",
                            "url": "admin/menu/index",
                            "node": "admin/menu/index",
                            "icon": "layui-icon layui-icon-layouts",
                            "params": ""
                        }
                    ]
                }
            ]
        }
    ]';
    PhinxExtend::write2menu(json_decode($json, true) ?: []);
    }
}
